package com.platform.user.model;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.common.entity.BaseDO;
import com.platform.common.entity.UserSessionDTO;
import com.platform.user.dto.UserDTO;
import com.platform.user.vo.LoginVO;
import com.platform.user.vo.UserInfoVO;
import com.platform.user.vo.UserVO;
import lombok.*;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;

/**
 * @author GH
 **/

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName(UserDOField.TABLE_NAME)
public class UserDO extends BaseDO {

    @TableId(value = UserDOField.ID, type = IdType.ASSIGN_ID)
    private String id;

    @TableField(value = UserDOField.CREATED_TIME, fill = FieldFill.INSERT)
    private Date createdTime;

    @TableField(value = UserDOField.UPDATED_TIME, fill = FieldFill.INSERT_UPDATE)
    private Date updatedTime;

    @TableField(value = UserDOField.DELETED)
    @TableLogic(value = "0", delval = "1")
    private Integer deleted;

    @TableField(value = UserDOField.ACCOUNT)
    private String account;

    @TableField(value = UserDOField.NICKNAME)
    private String nickname;

    @TableField(value = UserDOField.PASSWORD)
    private String password;

//    @TableField(value = UserDOField.EMAIL)
//    private String email;

//    @TableField(value = UserDOField.GENDER)
//    private String gender;

    @TableField(value = UserDOField.ROLE)
    private String role;

    public static LoginVO toLoginVO(UserDO userDO, String token) {
        LoginVO loginVO = LoginVO.builder()
                .token(token)
                .build();
        BeanUtils.copyProperties(userDO, loginVO, "token");
        return loginVO;
    }

    public static UserSessionDTO toUserSessionDTO(UserDO userDO, String ipv4, String userAgent) {
        UserSessionDTO userSessionDTO = UserSessionDTO.builder()
                .ipv4(ipv4)
                .userAgent(userAgent)
                .build();
        BeanUtils.copyProperties(userDO, userSessionDTO, "ipv4", "userAgent");
        return userSessionDTO;
    }

    public static UserVO toUserVO(UserDO userDO) {
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDO, userVO);
        return userVO;
    }

    public static List<UserVO> toUserVOList(Page<UserDO> userDOPage) {
        return userDOPage.convert(UserDO::toUserVO).getRecords();
    }

    public static UserInfoVO toUserInfoVO(UserDO userDO) {
        UserInfoVO userInfoVO = new UserInfoVO();
        BeanUtils.copyProperties(userDO, userInfoVO);
        return userInfoVO;
    }

    public static UserDO from(UserDTO userDTO) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userDTO, userDO);
        return userDO;
    }
}